package com.shouhuobao.bhi.msgnotice;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.evaluate.EvaluateActivity;
import com.collectplus.express.evaluate.EvaluateCompleteActivity;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.IdCardInfoBean;
import com.collectplus.express.model.MsgNoticeBean;
import com.collectplus.express.parcel.ParcelDetailActivity;
import com.collectplus.express.parcel.ParcelMainActivity2;
import com.collectplus.express.parcel.ParcelSendActivity;
import com.shouhuobao.bhi.coupon.CouponListActivity;

import droid.frame.view.xlist.SListView;

/**
 * 消息通知
 *
 * @author coffee<br>
 *         2015-10-24上午10:58:00
 *         1 路由
 *         2 邮票
 *         3 投诉
 *         4 意见反馈
 *         5 节假日
 *         6 其他
 *         7 快递员取消订单
 *         8 取件通知
 *         9 出柜入柜
 *         10 评价 (未评价才可以跳转)
 *         11包裹支付成功
 *         12 包裹投递成功
 *         13 包裹揽收成功
 *         14 包裹签收
 *         15邮票购买成功
 *         16邮票过期提醒
 *         17 实名认证
 *         18 实名认证驳回
 *         19 火箭侠揽收包裹
 *         20火箭侠拒收包裹
 *         21超时赔付通知
 */

public class MsgNoticeActivity extends BaseActivity2 {
    private SListView mListView;
    private MsgNoticeAdapter mAdapter;
    private LinearLayout mEmptyMessage;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        AppHttp.getInstance().msgNoticeList(1);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.message_center);
        super.findViewById();
        getAppTitle().setCommonTitle("消息中心");
        mEmptyMessage = (LinearLayout) findViewById(R.id.empty_message);
        mListView = (SListView) this.findViewById(R.id.listview);
        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().msgNoticeList(1);
            }
        });

        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MsgNoticeBean item = (MsgNoticeBean) parent.getItemAtPosition(position);
                // 1、路由 -- 9出柜入柜
                if (item.getType() == 1 || item.getType() == 7 || item.getType() == 9) {
                    // Intent intent = new Intent();
                    // intent.setClass(getContext(), ParcelSendDetailActivity.class);
                    // String orderId = item.getOrderId();
                    // if (orderId != null) {
                    // intent.putExtra("orderId", orderId);
                    // startActivity(intent);
                    // }
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ParcelDetailActivity.class);
                    int parcelId = item.getParcelId();
                    if (parcelId != 0) {
                        intent.putExtra("parcelId", parcelId + "");
                        startActivity(intent);
                    }
                }
                // 2 邮票
                else if (item.getType() == 2) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), CouponListActivity.class);
                    startActivity(intent);
                }
                // 8 取件通知
                else if (item.getType() == 8) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ParcelMainActivity2.class);
                    intent.putExtra("from", MsgNoticeActivity.class.getName());
                    intent.putExtra("parcelType", "2");
                    startActivity(intent);
                }
                // 10 评价 (未评价才可以跳转)
                else if (item.getType() == 10) {
                    // 未评价
                    if (item.getIsOrNoEvaluate() == 0) {
                        Intent intent = new Intent();
                        intent.setClass(getContext(), EvaluateActivity.class);
                        String orderId = item.getOrderId();
                        if (orderId != null) {
                            intent.putExtra("orderId", orderId);
                            startActivity(intent);
                        }
                    } else {
                        Intent intent = new Intent();
                        intent.setClass(getContext(), EvaluateCompleteActivity.class);
                        String orderId = item.getOrderId();
                        if (orderId != null) {
                            intent.putExtra("orderId", orderId);
                            startActivity(intent);
                        }
                        // showToast("您已评价");
                    }
                }
                //16邮票过期提醒 15邮票购买成功
                else if (item.getType() == 16 || item.getType() == 15) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), CouponListActivity.class);
                    startActivity(intent);
                }
                //11包裹支付成功 12 包裹投递成功 13 包裹揽收成功 14 包裹签收  19火箭侠揽收包裹
                else if (item.getType() == 11 || item.getType() == 12 || item.getType() == 13 || item.getType() == 14 || item.getType() == 19) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ParcelDetailActivity.class);
                    int parcelId = item.getParcelId();
                    if (parcelId != 0) {
                        intent.putExtra("parcelId", parcelId + "");
                        startActivity(intent);
                    }
                }
                //17 实名认证 18 实名认证驳回
                else if (17 == item.getType() || 18 == item.getType()) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().queryIDCardInfo(2);
                }//20 包裹拒收
                else if (20 == item.getType()) {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), ParcelSendActivity.class);
                    //跳转到待寄出包裹列表
                    intent.putExtra("item",3);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.msg_notice_list:
                onRefreshComplete();
                final AppResult<MsgNoticeBean> appResult = JSON.parseList(msg.obj, MsgNoticeBean.class);
                if (appResult.isSuccess()) {
                    getContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (appResult.getPageNum() == 1) {
                                if (appResult.getResultList().size() > 0) {
                                    mEmptyMessage.setVisibility(View.GONE);
                                    mSwipeLayout.setVisibility(View.VISIBLE);
                                    mAdapter = new MsgNoticeAdapter(appResult.getResultList(), getContext(), mListView);
                                    mListView.setAdapter(mAdapter);
                                } else {
                                    mSwipeLayout.setVisibility(View.GONE);
                                    mEmptyMessage.setVisibility(View.VISIBLE);
                                }

                            } else {
                                mAdapter.appendItems(appResult.getResultList(), true);
                            }
                            mListView.updateFooter(appResult.getPageNum(), appResult.getTotalPage());
                        }
                    });
                } else {
                    showToast(appResult.getMessage());
                }
                return true;
            case MsgID.query_idCard_info:
                cancelLoadingDialog();
                if (2 != msg.arg1) {
                    return false;
                }
                final AppResult<IdCardInfoBean> result = JSON.parser(msg.obj, IdCardInfoBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result.isSuccess()) {
                            AppLogic.contextToAuthenticationClass(getContext(), result.getResult());
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return false;
            default:
                break;
        }
        return super.handleMessage(msg);
    }

    @Override
    public void finish() {
        AppHttp.getInstance().msgNoticeSetRead();
        AppCache.clearAppMsgNum();
        setResult(RESULT_OK);
        super.finish();
    }
}
